package com.example.zy5_project_jetpack.mvp.view.SofaFragment.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Bean.TextBean;

import java.util.ArrayList;
import java.util.List;

public class TextAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<TextBean.DataBeanX.DataBean> dataBeans = new ArrayList<>();

    public TextAdapter(Context context) {
        this.context = context;
    }

    public void addList(List<TextBean.DataBeanX.DataBean> dataBeans) {
        this.dataBeans.addAll(dataBeans);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.text_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        TextBean.DataBeanX.DataBean dataBean = dataBeans.get(position);
        RequestOptions options = new RequestOptions();
        options.transform(new CircleCrop());
        Glide.with(context).load(dataBean.getAuthor().getAvatar()).apply(options).into(viewHolder.iv_image_text);
        viewHolder.tv_text_text.setText(dataBean.getAuthor().getName());
        Object activityText = dataBean.getActivityText();
        Object cover = dataBean.getCover();
        if(activityText==null){
            viewHolder.tv_title_text.setVisibility(View.GONE);
        }else {

            viewHolder.tv_title_text.setText(activityText.toString());
        }
        if(cover==null){
            viewHolder.tv_search_text.setText("音乐时刻");
        }else {

            viewHolder.tv_search_text.setText(cover.toString());
        }

    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }


    public static
    class ViewHolder extends RecyclerView.ViewHolder{
        public View rootView;
        public ImageView iv_image_text;
        public TextView tv_text_text;
        public TextView tv_title_text;
        public TextView tv_search_text;
        public TextView radio_zan;
        public TextView radio_step;
        public TextView radio_comment;
        public TextView radio_search;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.iv_image_text = (ImageView) rootView.findViewById(R.id.iv_image_text);
            this.tv_text_text = (TextView) rootView.findViewById(R.id.tv_text_text);
            this.tv_title_text = (TextView) rootView.findViewById(R.id.tv_title_text);
            this.tv_search_text = (TextView) rootView.findViewById(R.id.tv_search_text);
            this.radio_zan = (TextView) rootView.findViewById(R.id.radio_zan);
            this.radio_step = (TextView) rootView.findViewById(R.id.radio_step);
            this.radio_comment = (TextView) rootView.findViewById(R.id.radio_comment);
            this.radio_search = (TextView) rootView.findViewById(R.id.radio_search);
        }

    }
}
